package com.ushine.web.component.validation.rule;

import com.ushine.web.component.validation.annotation.Money;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @Author: yls
 * @Date: 2021/01/13 21:56
 * @Description: 金额校验规则：测试自定义规则校验
 * @Version 1.0
 */
public class MoneyValidator implements ConstraintValidator<Money, Double> {
    /**
     * 表示金额的正则表达式
     * 格式必须为 00.00 标准格式
     */
    private String moneyReg = "^\\d+(\\.\\d{2})$";

    private Pattern moneyPattern = Pattern.compile(moneyReg);

    @Override
    public boolean isValid(Double value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return moneyPattern.matcher(value.toString()).matches();
    }

}
